package com.kexun;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.util.*;
import java.util.concurrent.TimeUnit;

public class Main {
    public static void main(String[] args) {
        System.out.println("==========欢迎使用科讯小红书自动化操作系统=========");
        xiaohongshu();
    }


    public static WebDriver createDriver() {
//        谷歌驱动mac
        System.getProperties().setProperty("webdriver.chrome.driver", "/Users/lidong/chromedriver");
        //windows
//        System.getProperties().setProperty("webdriver.chrome.driver","chromedriver.exe");
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.setExperimentalOption("useAutomationExtension", false);
        chromeOptions.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
        chromeOptions.addArguments("--disable-blink-features=AutomationControlled");//发现主要是这句是关键
        ChromeDriver chromeDriver = new ChromeDriver(chromeOptions);
        chromeDriver.manage().timeouts().pageLoadTimeout(100, TimeUnit.SECONDS);
//        chromeDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        chromeDriver.manage().timeouts().setScriptTimeout(1, TimeUnit.SECONDS);
        return chromeDriver;
    }


    public static void xiaohongshu() {
        Random random = new Random();
        Scanner scanner = new Scanner(System.in);
        WebDriver driver = createDriver();
        driver.get("https://www.xiaohongshu.com");
        while (true) {
            try {
                System.out.println("请先扫描登录,登录完成后打开需要点赞关注的笔记,输入1按回车开始");
                int option = scanner.nextInt();
                String authhref = driver.findElement(By.cssSelector(".author-container .author-wrapper a")).getAttribute("href");

                while (true) {
                    List<WebElement> elements = driver.findElements(By.cssSelector(".parent-comment>.comment-item"));
                    System.out.println(elements.size());
                    for (WebElement element : elements) {
                        try {
                            WebElement right = element.findElement(By.cssSelector(".right"));
                            WebElement a = right.findElement(By.cssSelector(".author a"));
                            String href = a.getAttribute("href");
                            String text = a.getText();
                            if (href.equals(authhref)) {
                                continue;
                            }
                            String content = right.findElement(By.cssSelector(".content")).getText();

                            WebElement element2 = right.findElement(By.cssSelector(".like"));
                            WebElement element1 = element2.findElement(By.cssSelector(".reds-icon.like-icon"));
                            String use = element1.findElement(By.cssSelector("use")).getAttribute("xlink:href");
                            if ("#liked".equals(use)) {
                                continue;
                            }
//                        //评论点赞
                            Thread.sleep((random.nextInt(3)) * 1000);
                            element2.click();
                            Thread.sleep((random.nextInt(3) + 3) * 1000);
//                        //点击头像
                            a.click();
//                        // 获取当前打开窗口的句柄
                            String originalWindow = driver.getWindowHandle();
//
                            // 循环遍历所有打开的窗口，切换到新窗口
                            for (String handle : driver.getWindowHandles()) {
                                if (!handle.equals(originalWindow)) {
                                    driver.switchTo().window(handle);
                                    try {
                                        WebElement element3 = driver.findElement(By.cssSelector(".follow-button"));
                                        String text1 = element3.getText();
                                        if (text1.equals("关注")) {
                                            Thread.sleep((random.nextInt(3) + 5) * 1000);
                                            element3.click();
                                            System.out.println("关注成功  " + text + ": " + " " + content + " \n");

                                            Thread.sleep((random.nextInt(3) + 1) * 1000);
                                        } else {
                                            System.out.println("已关注");
                                        }
                                    } catch (Exception e) {
                                        System.out.println("关注失败");
                                    }

                                    // 关闭新窗口
                                    driver.close();
                                    break;
                                }
                            }
                            driver.switchTo().window(originalWindow);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                }
            } catch (Exception e) {
                System.out.println("出差了");
            }

        }

    }
}